package com.young.msg.web;

import com.young.common.core.base.BaseController;
import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.msg.model.MailSenderConfig;
import com.young.msg.service.IMsgMailSenderConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
* 邮件发送客户端服务
* Created by wangjiyu on 2021-10-28.
*/
@Controller
@RequestMapping("/msg/mailSender")
public class MsgMailSenderConfigController extends BaseController {

    @Resource(name="mailSenderService")
    IMsgMailSenderConfigService service;

    /**
     * 获取数据列表
     * @param obj
     * @param request
     * @return
     */
    @Log("查询邮件发送客户端列表")
    @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object getList(MailSenderConfig obj, HttpServletRequest request) {
        List<MailSenderConfig> list = service.getList(obj);
        return Result.buildSuccess(list);
    }

    /**
     * 获取分页数据
     * @param obj
     * @param request
     * @return
     */
    @Log("分页查询邮件发送客户端列表")
    @RequestMapping(value = "/getPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object getPage(MailSenderConfig obj, HttpServletRequest request) {
        Page page = CommonUtil.getPageFromRequest(request);
        List<MailSenderConfig> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     */
    @Log("查询邮件发送客户端")
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object get(Long id, HttpServletRequest request) {
        MailSenderConfig obj = service.get(id);
        return Result.buildSuccess(obj);
    }

    /**
     * 统计数量
     * @param obj
     * @param request
     * @return
     */
    @Log("统计邮件发送客户端数量")
    @RequestMapping(value = "/count", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object count(MailSenderConfig obj, HttpServletRequest request) {
        return Result.buildSuccess(service.count(obj));
    }

    /**
     * 新增
     * @param obj
     * @param request
     * @return
     */
    @Log("新增邮件发送客户端")
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    public Object insert(MailSenderConfig obj, HttpServletRequest request) {
        //插入填充
        this.fillForInsert(obj);
        service.insert(obj);
        return Result.buildSuccess();
    }

    /**
     * 修改
     * @param obj
     * @param request
     * @return
     */
    @Log("修改邮件发送客户端")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(MailSenderConfig obj, HttpServletRequest request) {
        //更新填充
        this.fillForUpdate(obj);
        service.update(obj);
        return Result.buildSuccess();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     */
    @Log("删除邮件发送客户端")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public Object delete(String ids, Long id, HttpServletRequest request) {
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            Long[] idArrNew = Arrays.stream(idArr).map(Long::valueOf).toArray(Long[]::new);
            service.delete(idArrNew);
        } else if(id != null) {
            service.delete(id);
        } else {
            return Result.buildFail("删除失败:待删除编号无效!");
        }
        return Result.buildSuccess();
    }



}